package com.badlogic.androidgames.framework;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import com.badlogic.androidgames.framework.gl.Texture;
import com.badlogic.androidgames.framework.gl.TextureRegion;
import com.badlogic.androidgames.framework.impl.GLGame;
import com.badlogic.androidgames.framework.math.Rectangle;
import com.badlogic.androidgames.framework.math.Vector2;
import com.badlogic.androidgames.stringutil.MyString;
import com.badlogic.androidgames.util.Result;

public class MyMovableImageTextBox 
{	
	public ArrayList<MyLine> lines;
	
	public boolean inMovement;
	public float width, height;
	
	public float xTouchDown, yTouchDown;
	
    public Vector2 position;
    public Rectangle bounds;
    
    public float xText, yText;
    
    public double touchDownStart = 0.0;
    public boolean touchDownFlag = false;
    public double doubleTouchDownStart = 0.0;
    public boolean doubleTouchDownFlag = false;
    
    //parte per l'immagine centrale
    public URL urlImageContent;
    public Texture textureContent;
    public TextureRegion regionContent;
    public boolean toLoad = false;
    public float w,h,ratio;
    public float regionContentWidth,regionContentHeight;
    public String textContent;
    
    public Result resource;
    
    public boolean textTouchDown;
    
 	public MyString text = new MyString(3,11);
 	
 	
 	public boolean complete;
 	public boolean userCompletation;
 	public boolean edited;
 	
 	public boolean enabled;
    
    public void setUrlStr(String urlStr)
    {
    	try {
			urlImageContent = new URL(urlStr);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
    }
    
    public void loadImageContent(GLGame game)
    {
    	float imgX,imgY;
    	float width,height;
    	float imgWidth,imgHeight;
    	float ratioOriginal, ratioDesired;
    	width=100.0f; //percentuale
    	height=100.0f; //percentuale
    	
    	ratioDesired = width / height; 
    	textureContent = new Texture(game, urlImageContent);
		ratioOriginal = textureContent.width / textureContent.height;
		
		if(ratioOriginal < ratioDesired)
		{//prendo come width quella original e mi calcolo la height
			imgX  = 0.00f;
			imgWidth = textureContent.width;

			imgHeight = textureContent.width*height/width;
			imgY=((textureContent.height-imgHeight)/2);
		}
		else
		{
			imgY  = 0.00f;
			imgHeight = textureContent.height;
			
			imgWidth = textureContent.height*width/height;
			imgX=((textureContent.width-imgWidth)/2);
		}
    	
		regionContentWidth= imgWidth;
		regionContentHeight=imgHeight;
		
    	regionContent = new TextureRegion(
				textureContent, imgX, imgY,
				imgWidth,
				imgHeight);
    	
    	/*regionContent = new TextureRegion(
				textureContent, 0, 0,
				textureContent.width,
				textureContent.height);*/
    }
    
    public void reloadImageContent(GLGame game)
    {
    	textureContent.reload();    	
    }
    
    public MyMovableImageTextBox(float x, float y, float width, float height) {
    	this.lines = new ArrayList<MyLine>();
    	
        this.position = new Vector2(x,y);
        this.width = width;
        this.height = height; 
        this.bounds = new Rectangle(x-width/2, y-height/2, width, height);

        this.inMovement = false;
        this.xTouchDown = 0.0f; 
     	this.yTouchDown = 0.0f;
     	     	
     	xText = (x-(width/2))+(0.07f*width);
     	yText = (y-(height/2)+(0.20f*height));
     	
     	textureContent = null;
		regionContent = null;
		
		textContent = null;
		
		complete = false;
		userCompletation = false;
		edited = false;
    }
    
    public void translateTo(float x, float y)
    {
    	this.position.x = this.position.x + (x - this.xTouchDown);
    	this.position.y = this.position.y + (y - this.yTouchDown);
    	this.bounds.lowerLeft.x = this.bounds.lowerLeft.x + (x - this.xTouchDown);
    	this.bounds.lowerLeft.y = this.bounds.lowerLeft.y + (y - this.yTouchDown);
    	this.xText = this.xText + (x - this.xTouchDown);
    	this.yText = this.yText + (y - this.yTouchDown);
    	
    	this.xTouchDown = x;
    	this.yTouchDown = y;
    }
    
    //quando la sposto attraverso la linea
    public void translateAt(float x, float y)
    {
    	this.bounds.lowerLeft.x = this.bounds.lowerLeft.x + (x - this.position.x);
    	this.bounds.lowerLeft.y = this.bounds.lowerLeft.y + (y - this.position.y);
    	this.xText = this.xText + (x - this.position.x);
    	this.yText = this.yText + (y - this.position.y);
    	this.position.x = x;
    	this.position.y = y;	
    }
    
    public void setTouchDownPosition(float x, float y)
    {
    	this.xTouchDown = x; 
     	this.yTouchDown = y;
    }
}
